package com.yylending.las.authority.password;

import android.text.TextUtils;
import com.yyfax.common.CLog;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.Model;
import com.yyfax.infrastructure.mvp.PresenterFactory;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yylending.las.authority.rsa.RSAEncodePresenter;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by laixj on 2017/5/26.
 * Copyright ©2016 www.yylending.com. All Rights Reserved.
 */
public class ResetPasswordPresenter extends RSAEncodePresenter<Model, ResetPasswordPresenter.View> {
	private final static String TAG = "ResetPasswordPresenter";

	public static class Generator implements PresenterFactory<ResetPasswordPresenter> {
		@Override
		public ResetPasswordPresenter create() {
			return new ResetPasswordPresenter();
		}
	}

	public interface View extends IView {
		void resetPwdSuccess();

		void resetPwdFail(String retCode, String msg);

		void resetPwdRetCode(String retCode);
	}

	private View mView;
	private String mRequestPage;

	@Override
	public void attach(ResetPasswordPresenter.View view) {
		mRequestPage = view.getClass().getName();
		mView = view;
		mModel = new ResetPasswordModel();
		EventAgent.register(this);
	}

	@Override
	public void detach() {
		mView = null;
		mModel = null;
		EventAgent.unregister(this);
	}

	@Override
	public Object getRSAParams(Object params, int functionalCode) {
		return params;
	}

	public void resetPassword(String phone, String newPassword, String verify) {
		execute(new ResetPasswordModel.ResetPassword(phone, newPassword, verify), ResetPasswordModel.METHOD_RESET_PASSWORD, true, null, mRequestPage);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void resetPwdResult(ResetPasswordModel.ResetPwdResult result) {
		if (mRequestPage.equals(result.requestPage)) {
			if (BaseResultEntity.isResponseSuccess(result)) {
				CLog.i(TAG, "重置密码成功");
				mView.resetPwdSuccess();
			} else {
				String msg = result.getRetMsg();
				mView.resetPwdFail(result.getRetCode(), TextUtils.isEmpty(msg) ? "重置密码失败" : msg);
			}
			mView.resetPwdRetCode(result.getRetCode());
		}
	}
}
